package com.zx.tradeclient.ctrl.index5;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.base.crop.Crop;
import com.zx.base.ctrl.BaseActivity;
import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.lazylist.ImageLoader;
import com.zx.base.util.ActivityTaskManager;
import com.zx.base.util.URLHelper;
import com.zx.base.widget.QuitDialog;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.entity.CategoryInfo;
import com.zx.tradeclient.entity.UserInfo;

public class UserCenterActivity extends BaseActivity implements OnClickListener {

	private TradeApp app;
	private ImageLoader imageLoader;
	private Button header_left, header_right;
	private ImageView usercenter_icon_header;
	private TextView usercenter_text_company, usercenter_text_loginname, usercenter_text_creditrating;

	private LinearLayout line_0, line_1, line_2, line_3, line_4;
	private Crop crop;
	private static final int CROP_FROM_CAMERA = 2;
	private boolean changedPhoto;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (TradeApp) getApplication();
		crop = new Crop(this);

		setContentView(R.layout.usercenter);
		if (!(ActivityTaskManager.getInstance().containsName("UserCenterActivity"))) {
			ActivityTaskManager.getInstance().putActivity("UserCenterActivity", this);
		}
		imageLoader = app.imageLoader;
		line_0 = (LinearLayout) findViewById(R.id.usercenter_line_0);
		line_1 = (LinearLayout) findViewById(R.id.usercenter_line_1);
		line_2 = (LinearLayout) findViewById(R.id.usercenter_line_2);
		line_3 = (LinearLayout) findViewById(R.id.usercenter_line_3);
		line_4 = (LinearLayout) findViewById(R.id.usercenter_line_4);

		usercenter_icon_header = (ImageView) findViewById(R.id.usercenter_icon_header);
		usercenter_text_company = (TextView) findViewById(R.id.usercenter_text_company);
		usercenter_text_loginname = (TextView) findViewById(R.id.usercenter_text_loginname);
		usercenter_text_creditrating = (TextView) findViewById(R.id.usercenter_text_creditrating);

		crop.setImageView(usercenter_icon_header);
		usercenter_icon_header.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				crop.showDialog();
			}
		});

		line_0.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 企业信息
				app.product_rating = new CategoryInfo();
				app.categ_index = 7;
				Intent intent = new Intent(UserCenterActivity.this, DetialPageActivity.class);
				startActivity(intent);
			}
		});
		line_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 我的供求
				Intent intent = new Intent(UserCenterActivity.this, SandDPageActivity.class);
				startActivity(intent);
			}
		});
		line_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 我的订单
				Intent intent = new Intent(UserCenterActivity.this, OrderPageActivity.class);
				startActivity(intent);
			}
		});
		line_3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 我的收藏
				Intent intent = new Intent(UserCenterActivity.this, FavoritePageActivity.class);
				startActivity(intent);
			}
		});
		line_4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 注销
				app.user = new UserInfo();
				Intent intent = new Intent(UserCenterActivity.this, LoginActivity.class);
				startActivity(intent);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		init();
	}

	private void init() {
		if (app.user.getId() == null || app.user.getId().equals("")) {
			Intent intent = new Intent(UserCenterActivity.this, LoginActivity.class);
			startActivity(intent);
		} else {
			setHeader(5);
			if (!changedPhoto)
				imageLoader.DisplayImage(URLHelper.mPrefixImg + "/" + app.user.getIcon(), usercenter_icon_header, R.drawable.icon_header);

			usercenter_text_company.setText(app.user.getCompany());
			usercenter_text_loginname.setText("会员ID:" + app.user.getLoginName());
			usercenter_text_creditrating.setText("会员等级:" + app.user.getCreditRating());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != Activity.RESULT_OK)
			return;
		crop.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case CROP_FROM_CAMERA:
			Bundle extras = data.getExtras();
			if (extras != null) {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("act", "iconModify");
				params.put("id", app.user.getId());
				Bitmap bitmap = ((BitmapDrawable) usercenter_icon_header.getDrawable()).getBitmap();
				params.put("photo", bitmap);
				new Upload().execute(params);
				changedPhoto = true;
			}
			break;
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {

		} else if (v == header_right) {

		}

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (Constant.debug)
				Log.d("TMG", "ManageActivity KEYCODE_BACK");
			QuitDialog dialog = new QuitDialog(UserCenterActivity.this);
			dialog.exitConfirm();

			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	class Upload extends AsyncTask<Map<String, Object>, Void, String> {

		@Override
		protected String doInBackground(Map<String, Object>... params) {
			return HttpUtils.postImage(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			if(Constant.debug)Log.d("debug", result);
		}

		@Override
		protected void onCancelled() {
			if(Constant.debug)Log.d("debug", "onCancelled");
		}

	}
}